﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BUS;
using DTO;

namespace nhahangbarota
{
    public partial class Formchinh : Form
    {
        public Formchinh()
        {
            InitializeComponent();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void Formchinh_Load(object sender, EventArgs e)
        {
            this.Refresh();
            FormChinhBUS BUS = new FormChinhBUS();
            listBox1.DataSource = BUS.Loadhoadon();
            listBox1.DisplayMember = "MaHoaDon";
            listBox1.ValueMember = "MaHoaDon";

            cbBan.DataSource = BUS.Loadbanan();
            cbBan.DisplayMember = "TenBan";
            cbBan.ValueMember = "MaBan";

            cbNV.DataSource = BUS.LoadNV();
            cbNV.DisplayMember = "TenNV";
            cbNV.ValueMember = "MaNV";

            txtVAT.Text = BUS.LoadVAT().Rows[0][0].ToString();

            //Column2.DataSource = BUS.Loadtenmon();
            //Column2.DisplayMember = "TenMon";
            //Column2.ValueMember = "MaMon";

            foreach (DataRow dtRow in BUS.Loadtenmon().Rows)
            {
                Column2.Items.Add(dtRow[1].ToString());
                Column2.ValueMember = dtRow[0].ToString();
            }
            int tong = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                dataGridView1.Rows[i].Cells[0].Value = i + 1;
                tong = tong + Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
                txttongtien.Text = tong.ToString();
            }

        }

        private void rdosang_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void rdotoi_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            FormChinhBUS BUS = new FormChinhBUS();
            DataTable a = new DataTable();
            DataTable b = new DataTable();
            DataRowView drView = (DataRowView)listBox1.SelectedItem;
            DataRow dr = drView.Row;
            int mahoadon = Convert.ToInt32(dr["MaHoaDon"]);

            FormChinhDTO DTO = new FormChinhDTO(mahoadon);
            a = BUS.Loadchitiethoadon(DTO);


            txtKhach.Text = a.Rows[0][1].ToString();
            //txtmahoadon.Text = a.Rows[0][0].ToString();
            cbBan.SelectedValue = a.Rows[0][4].ToString();
            cbNV.SelectedValue = a.Rows[0][3].ToString();
            dateNgay.Text = a.Rows[0][2].ToString();
            if (a.Rows[0][6].ToString() == "0")
            {
                rdosang.Checked = true;
            }
            else
            {
                rdotoi.Checked = true;
            }

            txttongtien.Text = a.Rows[0][5].ToString();
            b = BUS.Loadchitiethoadonmonan(DTO);
            if (b.Rows.Count > 0)
            {
                foreach (DataRow dtRow in b.Rows)
                {

                    dataGridView1.Rows.Add(dataGridView1.Rows.Count.ToString(), dtRow[0].ToString(), dtRow[1].ToString(), dtRow[2].ToString(), dtRow[3].ToString());

                }

                double tong = 0;
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = i + 1;
                    tong = tong + Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value);
                    txttongtien.Text = tong.ToString();
                }

                double VAT = (((Convert.ToInt32(BUS.LoadVAT().Rows[0][0].ToString()) + 100) * 0.1) / (100 * 0.1));
                double thanhtien = Convert.ToInt32(txttongtien.Text) * VAT;
                txtthanhtien.Text = thanhtien.ToString();
            }
            else
            {
            }
        }

        private void vATToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            VAT A = new VAT();
            A.Show();

        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            FormChinhBUS BUS = new FormChinhBUS();
            //int dem = 0;
            //for (int i = 0; i < BUS.Loadhoadon().Rows.Count; i++)
            //{
            //    if (BUS.Loadhoadon().Rows[i][0].ToString() == txtmahoadon.Text)
            //    {
            //        dem++;
            //    }
            //}
            //if (dem == 0)
            //{
            FormChinhDTO DTO;
            if (rdosang.Checked == true)
            {
                DTO = new FormChinhDTO(dateNgay.Text, Convert.ToInt32(cbBan.SelectedValue), Convert.ToInt32(cbNV.SelectedValue), txtKhach.Text, Convert.ToDouble(txttongtien.Text), 0);
            }
            else
            {
                DTO = new FormChinhDTO(dateNgay.Text, Convert.ToInt32(cbBan.SelectedValue), Convert.ToInt32(cbNV.SelectedValue), txtKhach.Text, Convert.ToDouble(txttongtien.Text), 1);
            }
            BUS.Themhoadon(DTO);





            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                FormChinhDTO DTO2 = new FormChinhDTO(dataGridView1.Rows[i].Cells[1].Value.ToString());
                FormChinhDTO DTO3 = new FormChinhDTO(Convert.ToInt32(BUS.max().Rows[0][0].ToString()), Convert.ToInt32(BUS.xuatmamonan(DTO2).Rows[0][0].ToString()), Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value.ToString()), Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value.ToString()));
                BUS.Themchitiethoadon(DTO3);
            }
            listBox1.DataSource = BUS.Loadhoadon();
            listBox1.DisplayMember = "MaHoaDon";
            listBox1.ValueMember = "MaHoaDon";
            //}
            //else
            //{
            //    MessageBox.Show("Mã Hoá đơn này đã tồn tại");
            //}
        }

        private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {

        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {


        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            double tong = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                dataGridView1.Rows[i].Cells[0].Value = i + 1;
                tong = tong + Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value);
                txttongtien.Text = tong.ToString();
            }
            FormChinhBUS BUS = new FormChinhBUS();
            double VAT = (((Convert.ToInt32(BUS.LoadVAT().Rows[0][0].ToString()) + 100) * 0.1) / (100 * 0.1));
            double thanhtien = Convert.ToInt32(txttongtien.Text) * VAT;
            txtthanhtien.Text = thanhtien.ToString();

        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn thoát", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.Close();
                
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn xoá", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (listBox1.Items.Count > 0)
                {
                    FormChinhBUS BUS = new FormChinhBUS();
                    FormChinhDTO DTO = new FormChinhDTO(Convert.ToInt32(listBox1.SelectedValue));
                    BUS.xoahoadon(DTO);
                    listBox1.DataSource = BUS.Loadhoadon();
                    listBox1.DisplayMember = "MaHoaDon";
                    listBox1.ValueMember = "MaHoaDon";
                }
                else
                {
                    MessageBox.Show("Không có gì để xoá");
                }
            }
        }

        private void txtmahoadon_TextChanged(object sender, EventArgs e)
        {


        }

        private void txtmahoadon_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            FormChinhBUS BUS = new FormChinhBUS();
            FormChinhDTO DTO = new FormChinhDTO(datetimkiem.Text);

            listBox1.DataSource = BUS.Loadhoadonngay(DTO);
            listBox1.DisplayMember = "MaHoaDon";
            listBox1.ValueMember = "MaHoaDon";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FormChinhBUS BUS = new FormChinhBUS();
            listBox1.DataSource = BUS.Loadhoadon();
            listBox1.DisplayMember = "MaHoaDon";
            listBox1.ValueMember = "MaHoaDon";
        }

        private void btnsua_Click(object sender, EventArgs e)
        {

            DataTable a = new DataTable();
            DataRowView drView = (DataRowView)listBox1.SelectedItem;
            DataRow dr = drView.Row;
            int mahoadon = Convert.ToInt32(dr["MaHoaDon"]);
            FormChinhDTO DTO4 = new FormChinhDTO(mahoadon);
            FormChinhBUS BUS = new FormChinhBUS();
            FormChinhDTO DTO;
            if (rdosang.Checked == true)
            {
                DTO = new FormChinhDTO(dateNgay.Text, Convert.ToInt32(cbBan.SelectedValue), Convert.ToInt32(cbNV.SelectedValue), txtKhach.Text, Convert.ToDouble(txttongtien.Text), 0);
            }
            else
            {
                DTO = new FormChinhDTO(dateNgay.Text, Convert.ToInt32(cbBan.SelectedValue), Convert.ToInt32(cbNV.SelectedValue), txtKhach.Text, Convert.ToDouble(txttongtien.Text), 1);
            }
            BUS.suahoadon(DTO);




            BUS.xoachitiethet(DTO4);
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {

                FormChinhDTO DTO2 = new FormChinhDTO(dataGridView1.Rows[i].Cells[1].Value.ToString());
                FormChinhDTO DTO3 = new FormChinhDTO(Convert.ToInt32(mahoadon), Convert.ToInt32(BUS.xuatmamonan(DTO2).Rows[0][0].ToString()), Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value.ToString()), Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value.ToString()));
                BUS.Themchitiethoadon(DTO3);
            }




            //a = BUS.Loadchitiethoadonmonan(DTO4);

            //if (a.Rows.Count < dataGridView1.Rows.Count - 1)
            //{
            //    for (int i = a.Rows.Count; i < dataGridView1.Rows.Count - 1; i++)
            //    {
            //        FormChinhDTO DTO2 = new FormChinhDTO(dataGridView1.Rows[i].Cells[1].Value.ToString());
            //        FormChinhDTO DTO3 = new FormChinhDTO(Convert.ToInt32(txtmahoadon.Text), Convert.ToInt32(BUS.xuatmamonan(DTO2).Rows[0][0].ToString()), Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value.ToString()), Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value.ToString()));
            //        BUS.Themchitiethoadon(DTO3);
            //    }

            //}

            //for (int i = 0; i < a.Rows.Count; i++)
            //{
            //    FormChinhDTO DTO2 = new FormChinhDTO(dataGridView1.Rows[i].Cells[1].Value.ToString());
            //    FormChinhDTO DTO3 = new FormChinhDTO(Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value.ToString()), Convert.ToInt32(txtmahoadon.Text), Convert.ToInt32(BUS.xuatmamonan(DTO2).Rows[0][0].ToString()), Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value.ToString()), Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value.ToString()));
            //    BUS.suachitiethoadon(DTO3);
            //}

        }

        private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            About ab = new About();
            ab.Show();
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {

        }

        private void dataGridView1_NewRowNeeded(object sender, DataGridViewRowEventArgs e)
        {

        }

        private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {


        }

        private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
        {

        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {


        }

        private void thêmBànToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            ThemBan a = new ThemBan();
            a.ShowDialog();
            
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn thoát", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void doanhThuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DoanhThu frm = new DoanhThu();
            frm.Show();
        }

        private void xoáSửaBànToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            BanAn a = new BanAn();
            a.ShowDialog();
            
        }

        private void thêmMónĂnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            ThemMon a = new ThemMon();
            a.ShowDialog();
            
        }

        private void xoásửaMónĂnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            MonAn a = new MonAn();
            a.ShowDialog();
            
        }

        private void thêmNhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            ThemNV a = new ThemNV();
            a.ShowDialog();
           
        }

        private void xoásửaNhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            NhanVien a = new NhanVien();
            a.ShowDialog();
            
        }
    }
}
